//
// Copyright © 2021 dev. All rights reserved.
// 

import Foundation
import ConfCore
import RealmSwift
import os.log
import CloudKit

final class LiveObserver:NSObject {
    private let log = OSLog(subsystem: "WWDC", category: "LiveObserver")
    private let dateProvider:DateProvider
    private let storage:Storage
    private let syncEngine:SyncEngine
    private var refreshTimer:Timer?
    var isRunning = false
    private let specialEventsObserver:CloudKitLiveObserver
}



private final class CloudKitLiveObserver {
    private let log = OSLog(subsystem: "WWDC", category: "CloudKitLiveObserver")
    private let storage:Storage
    private lazy var database: CKDatabase = CKContainer.default().publicCloudDatabase
    init(storage:Storage) {
        self.storage = storage

    }

    private func createSubscriptionIfNeeded(){
        #if ICLOUD
        CloudKitHelper.subscriptionExists(with: specialLiveEventsSubscriptionID, in: database) { [unowned self] exists in
            if !exists {
                self.doCreateSubscription()
            }
        }
        #endif
    }

    private func doCreateSubscription(){
        #if ICLOUD
        #endif
        let options:CKQuerySubscription.Options = [.firesOnRecordCreation,.firesOnRecordUpdate,.firesOnRecordDeletion]
//        let subscription = CKQuerySubscription(recordType: Ses, predicate: <#T##NSPredicate#>)
    }
}
